package com.powernode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.constant.MqConst;
import com.powernode.domain.Order;
import com.powernode.domain.PaymentInfo;
import com.powernode.feign.PayOrderFeign;
import com.powernode.mapper.PaymentInfoMapper;
import com.powernode.model.Result;
import com.powernode.service.PaymentService;
import com.powernode.service.RabbitService;
import com.wechat.pay.java.service.payments.model.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

import static com.alibaba.fastjson2.JSON.toJSONString;

@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentService {

    @Autowired
    private PayOrderFeign payOrderFeign;

    @Autowired
    private RabbitService rabbitService;

    @Override
    public PaymentInfo savePaymentInfo(String orderNumber) {
        // 1.根据订单编号出现支付记录
        PaymentInfo paymentInfo = baseMapper.selectOne(new LambdaQueryWrapper<PaymentInfo>()
                .eq(PaymentInfo::getOrderNo, orderNumber));

        if (paymentInfo != null && paymentInfo.getPaymentStatus() == 1) {
            throw new RuntimeException("该订单已支付");
        }

        // 2.如果支付记录不存在，就进行添加
        if (paymentInfo == null) {
            // 3.远程调用：根据订单号查询订单数据
            Result<Order> result = payOrderFeign.getByOrderNumber(orderNumber);
            Order orderInfo = result.getData();

            // 4.添加数据
            paymentInfo = new PaymentInfo();
            paymentInfo.setUserId(orderInfo.getOpenId());
            paymentInfo.setContent("商品购买订单");
            paymentInfo.setAmount(orderInfo.getTotalMoney());
            paymentInfo.setOrderNo(orderNumber);
            paymentInfo.setPaymentStatus(0);
            baseMapper.insert(paymentInfo);
        }
        return paymentInfo;
    }

    // 支付成功后，后续处理
    @Override
    public void updatePaymentStatus(Transaction transaction) {
        // 1.更新支付记录状态：已支付
        String outTradeNo = transaction.getOutTradeNo();
        // 根据订单编号查询支付记录
        PaymentInfo paymentInfo = baseMapper.selectOne(new LambdaQueryWrapper<PaymentInfo>()
                .eq(PaymentInfo::getOrderNo, outTradeNo));

        // 判断如果支付记录状态已经修改了，不需要操作
        Integer paymentStatus = paymentInfo.getPaymentStatus();
        if (paymentStatus.intValue() == 1) { // 已经修改状态
            return;
        }

        // 没有修改，再进行修改
        paymentInfo.setPaymentStatus(1);
        paymentInfo.setOrderNo(transaction.getOutTradeNo());
        paymentInfo.setTransactionId(transaction.getTransactionId());
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(toJSONString(transaction));
        baseMapper.updateById(paymentInfo);

        // 2.基于MQ通知订单系统，修改订单状态
        rabbitService.sendMessage(MqConst.EXCHANGE_PAYMENT_PAY,
                MqConst.ROUTING_PAYMENT_PAY,
                paymentInfo.getOrderNo());
    }
}
